

class CheckSetting {
  constructor() {

  }


  /*++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
  检查有没有授权，
  -----------------------------------------------------------------------------------------------------------------------------------------------------------------------++*/
  checkSetting({scopeType,authorize,successCallBack,}){
    let self = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.' + scopeType]) {
            successCallBack();
        }else {
          (authorize && typeof authorize ==="function" ) ? authorize() : self.authorize(scopeType,successCallBack,self);
        }
      },
      fail(){
      },
      complete(res){
      }
    })
  }
  /*++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
  授权处理方法
  -----------------------------------------------------------------------------------------------------------------------------------------------------------------------++*/
  authorize(scopeType,successCallBack,self) {
    console.log('test');
    wx.authorize({
      scope: 'scope.' + scopeType,
      success(res) {
        successCallBack()
      },
      fail() {
        wx.showModal({
          title: '提示',
          content: '请允许授权以便为你提供更好的服务',
          showCancel: false,
          success: function () {
            wx.openSetting({
              success: (res) => {
                if (res.authSetting['scope.' + scopeType]) {
                  successCallBack()
                } else {
                  self.checkSetting({scopeType,successCallBack})
                }
              },
              fail:(res)=> {
                self.checkSetting({scopeType,successCallBack})
              }
            });
          }
        })
      }
    })
  }

}

export default new CheckSetting ;
